package cn.iocoder.yudao.module.resource.service.audiocommand;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.redis.core.RedisPublishClient;
import cn.iocoder.yudao.module.resource.events.RefreshAudioCommandEvent;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import cn.iocoder.yudao.module.resource.controller.admin.audiocommand.vo.*;
import cn.iocoder.yudao.module.resource.dal.dataobject.audiocommand.AudioCommandDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.resource.dal.mysql.audiocommand.AudioCommandMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.resource.enums.ErrorCodeConstants.*;

/**
 * 语音指令配置 Service 实现类
 *
 * @author cai
 */
@Service
@Validated
public class AudioCommandServiceImpl implements AudioCommandService {

    @Resource
    private AudioCommandMapper audioCommandMapper;
    @Resource
    private RedisPublishClient redisPublishClient;

    @Override
    public AudioCommandDO getAudioCommandByTitle(String title) {
        return audioCommandMapper.selectOne(
                new LambdaQueryWrapperX<AudioCommandDO>()
                        .eq(AudioCommandDO::getCmdTitle, title)
                        .eq(AudioCommandDO::getDeleted, false)
        );
    }

    @Override
    public Long createAudioCommand(AudioCommandSaveReqVO createReqVO) {
        // 插入
        AudioCommandDO audioCommand = BeanUtils.toBean(createReqVO, AudioCommandDO.class);
        audioCommand.setSensorType(audioCommand.getSensorType().trim());
        audioCommand.setOperateType(audioCommand.getOperateType().trim());
        audioCommandMapper.insert(audioCommand);

        redisPublishClient.publishMsg(new RefreshAudioCommandEvent());

        // 返回
        return audioCommand.getId();
    }

    @Override
    public void updateAudioCommand(AudioCommandSaveReqVO updateReqVO) {
        // 校验存在
        validateAudioCommandExists(updateReqVO.getId());
        // 更新
        AudioCommandDO audioCommand = BeanUtils.toBean(updateReqVO, AudioCommandDO.class);
        audioCommand.setSensorType(audioCommand.getSensorType().trim());
        audioCommand.setOperateType(audioCommand.getOperateType().trim());
        audioCommandMapper.updateById(audioCommand);

        redisPublishClient.publishMsg(new RefreshAudioCommandEvent());
    }

    @Override
    public void deleteAudioCommand(Long id) {
        // 校验存在
        validateAudioCommandExists(id);
        // 删除
        audioCommandMapper.deleteById(id);

        redisPublishClient.publishMsg(new RefreshAudioCommandEvent());
    }

    private void validateAudioCommandExists(Long id) {
        if (audioCommandMapper.selectById(id) == null) {
            throw exception(AUDIO_COMMAND_NOT_EXISTS);
        }
    }

    @Override
    public AudioCommandDO getAudioCommand(Long id) {
        return audioCommandMapper.selectById(id);
    }

    @Override
    public PageResult<AudioCommandDO> getAudioCommandPage(AudioCommandPageReqVO pageReqVO) {
        return audioCommandMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AudioCommandDO> getAvailableAudioCommandList() {
        return audioCommandMapper.selectList(
                new LambdaQueryWrapperX<AudioCommandDO>()
                        .eq(AudioCommandDO::getEnable, true)
                        .eq(AudioCommandDO::getDeleted, false)
        );
    }

    @Override
    public AudioCommandDO getAudioCommandByOperateType(String sensorType, String operateType) {
        return audioCommandMapper.selectOne(
                new LambdaQueryWrapperX<AudioCommandDO>()
                        .eq(AudioCommandDO::getSensorType, sensorType)
                        .eq(AudioCommandDO::getOperateType, operateType)
                        .eq(AudioCommandDO::getEnable, true)
                        .eq(AudioCommandDO::getDeleted, false)
        );
    }
}